// pages/fabu/fabu/index.js
const App = getApp()
const recorderManager = wx.getRecorderManager()
const uploadImage = require('../../../aliyun/uploadAliyun.js');
var init = '';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    voiceSrc:[],
    isSubmit: false,
    urllist:[],
    id:'',
    endImgs:[],
    money:0
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
     this.setData({
      id:options.id,
     })
  },
  voiceAdd: function () {
    let that = this
    that.chooseImage(that)
  },
  //选择图片
  chooseImage: function (that) {
    wx.chooseImage({
      count: 9, // 最多可以选择的图片张数，默认9
      sizeType: ['original', 'compressed'],
      sourceType: ['album', 'camera'], 
      success: function (res) {
       console.log(res)
       let voiceSrc = that.data.voiceSrc.concat(res.tempFilePaths);
        that.setData({
          voiceSrc
        })
      }
    })
     
},
//提交图片到oss
  bindFormSubmit:function(e){
    let that = this 
    if(that.data.money == 0){
      App.showError('请输入货款金额');
      return false;
    }
    if(that.data.voiceSrc == ''){
      App.showError('请上传图片');
      return false;
    }
    that.voiceDaka();
  },
  voiceDaka: function (title) {
    let that = this
      wx.showLoading({
        title: '上传中',
      })
      var i = 0; var length = that.data.voiceSrc.length;
					
      that.upload(that.data.voiceSrc,i,length)
    },
    //递归获取图片链接
			upload(tempFilePaths,i,length){
        var that=this;
        uploadImage(
          {
            filePath: that.data.voiceSrc[i],
            dir: 'yipingou/gongyingshang/',
            success: function (res) {
              console.log("上传成功")
              console.log(res) 
              let urllist= that.data.urllist
              urllist.push(res)		
              that.setData({
                urllist
              })
							i++
							if(i<length){
								that.upload(tempFilePaths,i,length)
							}else{
                // console.log(that.data.urllist)
                that.endSubmit()
              }
				
              wx.hideLoading()
            },
            fail: function (res) {
              wx.hideLoading()
              wx.showModal({
                title: '文件大小不能超过100M',
                content: '请重试',
                showCancel: false,
              })
            }
          })
					
      } ,
      //提交到数据库
      endSubmit(){
        var that=this;
        App._post_form('user/addImg', {
          imgs:that.data.urllist,
          money:that.data.money
        }, result => {
          App.showSuccess(result.data,function(){
            wx.redirectTo({
              url: '/pages/supplier/index',
            })
          })
        }, false, () => {
          wx.hideLoading();
        });
      },
    //删除图片
    closeimg(e){
      var that=this;
      let index=e.currentTarget.dataset.index
      let voiceSrc=that.data.voiceSrc
       voiceSrc.splice(index,1)
      that.setData({
        voiceSrc
      })
    },
     //图片预览
      previewImg(e){
        var that=this;
        let index=e.currentTarget.dataset.index
        let iindex=e.currentTarget.dataset.iindex
        wx.previewImage({
          current:that.data.endImgs[index].imgs[iindex],
          urls:that.data.endImgs[index].imgs,
        })
      },
      /**
   * 输入货款金额
   */
  contentInput: function(e) {
    let _this = this
    let index = e.currentTarget.dataset.index;
    _this.setData({
      money:e.detail.value
    })
  },
})